﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TaiwuModFramework.Common.Abstract;
using TaiwuModFramework.Common.Base;

namespace TaiwuModFramework.Common.Info {
    public class ConfigItemInfo {


        // 补丁对象
        public BaseTaiwuComp CompObj { get; }
        // 属性信息
        public PropertyInfo Property { get; }
        // 字段信息
        public FieldInfo Field { get; }
        // 配置文件中配置项的key
        public string ConfigItemKey { get; }
        // 配置文件中配置项的类型和key
        public string TypeAndKey { get; }
        // 配置项的类型
        public Type ConfigItemType { get; }
        // 配置项的值
        private object valueObj;


        public ALogger Log {
            get {
                return CompObj.Log ;
            }
        }
        public object Value {
            get { return valueObj; }
            set {
                if(Equals(valueObj, value)) return;
                if(CompObj == null) {
                    return;
                }
                if(Field != null) {
                    Field.SetValue(CompObj, value);
                } else if(Property != null) {
                    Property.SetValue(CompObj, value);
                }
                Log.Info("ConfigUpdate  {0}@{1}: {2} --> {3}", CompObj.GetType().Name, TypeAndKey, valueObj.ToString(), value.ToString());
                valueObj = value;
            }
        }


        public ConfigItemInfo(BaseTaiwuComp obj, PropertyInfo property, string ConfigKey) {
            ConfigItemKey = ConfigKey == null ? property.Name : ConfigKey;
            Property = property;
            ConfigItemType = property.PropertyType;
            TypeAndKey = CalcKey(ConfigItemType, ConfigKey);
            if(obj != null) {
                CompObj = obj;
                valueObj = property.GetValue(obj);
            }
        }



        public ConfigItemInfo(BaseTaiwuComp obj, FieldInfo field, string ConfigKey) {
            ConfigItemKey = ConfigKey == null ? field.Name : ConfigKey;
            Field = field;
            ConfigItemType = Field.FieldType;
            TypeAndKey = CalcKey(ConfigItemType, ConfigKey);
            if(obj != null) {
                CompObj = obj;
                valueObj = field.GetValue(obj);
            }
        }



        /// <summary>
        /// 计算配置项的key
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="ConfigKey">配置文件中的key</param>
        /// <returns></returns>
        public static string CalcKey(Type type, string ConfigKey) {
            return string.Format("{0}@{1}", type, ConfigKey);
        }
        public override bool Equals(object obj) {
            if(obj != null && Equals(this, obj)) {
                var bci = (ConfigItemInfo)obj;
                return TypeAndKey.Equals(bci.TypeAndKey) && CompObj.Equals(bci.CompObj);
            }
            return false;
        }
        public override string ToString() {
            return string.Format("({0} {1}={2})", ConfigItemType.Name, ConfigItemKey, Value);
        }

        public override int GetHashCode() {
            return (TypeAndKey + "@" + CompObj).GetHashCode();
        }
    }
}
